<template>
  <div class="home">
    <div class="title-container">
      <div class="search-content">
        <el-form :model="searchModel" :inline="true">
          <el-form-item>
            <el-input
              v-model="searchModel.userName"
              :disabled="detailsDisabled"
              placeholder="请输入组件编号"
              prefix-icon="el-icon-search"
            />
          </el-form-item>
        </el-form>
        <div class="search-btn">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="searchClick"
            size="medium"
          >搜索
          </el-button>
        </div>
      </div>
    </div>

    <el-card>
      <div class="addAccount">
        <perButton label="新建" type="primary" icon="el-icon-plus" size="medium" perms="PT_USER_ADD"
                   @click="handlerAccount"></perButton>
        <el-button
          type="success"
          icon="el-icon-refresh"
          size="medium"
          @click="refresh"
        >刷新
        </el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%;"
        class="elTable"
      >
        <el-table-column type="index" label="序号" width="50" align="center"/>
        <el-table-column prop="cardCode" label="组件编号" align="center"/>
        <el-table-column prop="cardName" label="组件名称" align="center"/>
        <el-table-column prop="cardSize" label="组件大小" align="center"/>
        <el-table-column
          label="组件预览"
          align="center"
          width="130"
        >
          <template #default="{ row }">
            <el-image
              style="width: 100px; height: 100px"
              fit="cover"
              :src="row.cardShowPic"
              :preview-src-list="[row.cardShowPic]">
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="cardPurpose" label="卡片用途" align="center">
          <template #default="{ row }">
            <span>{{cardTypeListData.find(item => item.value==row.cardPurpose).label}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="样式类型"
          align="center"
          width="130"
        >
          <template #default="{ row }">
            <span>{{styleTypeListData.find(item => item.value==row.uiStyle).label}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createByName" label="创建人" align="center"/>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column prop="updateByName" label="更新人" align="center"/>
        <el-table-column label="更新时间" align="center" prop="createTime">
          <template slot-scope="scope">
            {{ scope.row.updateTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="{ row }">
            <perButton label="编辑" type="warning" size="mini" perms="PT_USER_EDIT"
                       @click="handlerEdit(row)"></perButton>
            <perButton label="删除" type="danger" size="mini" perms="PT_USER_DEL"
                       @click="handlerDelete(row)"></perButton>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        class="pagination"
        :total="count"
        :page.sync="pageIndex"
        :limit.sync="pageSize"
        @pagination="getAccountData"
      />
    </el-card>

    <!-- 弹框 -->
    <yc-dialog ref="ycDialog" :dialogTitle="dialogTitle" :centerTitle="centerTitle">
      <template #content>
        <div class="dialog-content">
          <el-form
            ref="newAccountFormRef"
            :rules="newAccountModelRules"
            :model="newAccountModel"
            :disabled="detailsDisabled"
            label-width="140px"
          >
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="组件名称" prop="cardName">
                  <el-input v-model="newAccountModel.cardName"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="组件编号" prop="cardCode">
                  <el-input v-model="newAccountModel.cardCode"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="组件大小" prop="cardSize">
                  <el-select v-model="newAccountModel.cardSize" class="w-100">
                    <el-option
                      v-for="item in zjSizeListData"
                      :key="item"
                      :label="item"
                      :value="item"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="组件用途" prop="cardPurpose">
                  <el-select v-model="newAccountModel.cardPurpose" class="w-100">
                    <el-option
                      v-for="item in cardTypeListData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="组件主题" prop="uiStyle">
                  <el-select v-model="newAccountModel.uiStyle" class="w-100">
                    <el-option
                      v-for="item in styleTypeListData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="组件描述" prop="cardDes">
                  <el-input v-model="newAccountModel.cardDes"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="标题设置" prop="cardConfTitle">
                  <el-select v-model="newAccountModel.cardConfTitle" class="w-100">
                    <el-option
                      v-for="item in isShowData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="标题可填个数" prop="cardTitleNumber">
                  <el-input v-model="newAccountModel.cardTitleNumber"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="颜色设置" prop="cardConfColor">
                  <el-select v-model="newAccountModel.cardConfColor" class="w-100">
                    <el-option
                      v-for="item in isShowData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="颜色可填个数" prop="cardColorNumber">
                  <el-input v-model="newAccountModel.cardColorNumber"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="图标设置" prop="cardConfIcon">
                  <el-select v-model="newAccountModel.cardConfIcon" class="w-100">
                    <el-option
                      v-for="item in isShowData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="图标可填个数" prop="cardIconNumber">
                  <el-input v-model="newAccountModel.cardIconNumber"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="数值型变量设置" prop="cardConfInt">
                  <el-select v-model="newAccountModel.cardConfInt" class="w-100">
                    <el-option
                      v-for="item in isShowData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="数值型变量可填个数" prop="cardIntNumber">
                  <el-input v-model="newAccountModel.cardIntNumber"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="状态型变量设置" prop="cardConfStatus">
                  <el-select v-model="newAccountModel.cardConfStatus" class="w-100">
                    <el-option
                      v-for="item in isShowData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="状态型变量可填个数" prop="cardStatusNumber">
                  <el-input v-model="newAccountModel.cardStatusNumber"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="控制参数设置" prop="cardConfControl">
                  <el-select v-model="newAccountModel.cardConfControl" class="w-100">
                    <el-option
                      v-for="item in isShowData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="控制参数可填个数" prop="cardControlNumber">
                  <el-input v-model="newAccountModel.cardControlNumber"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="流程设置" prop="cardConfFlow">
                  <el-select v-model="newAccountModel.cardConfFlow" class="w-100">
                    <el-option
                      v-for="item in isShowData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="流程可填个数" prop="cardFlowNumber">
                  <el-input v-model="newAccountModel.cardFlowNumber"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="数据集设置" prop="cardConfData">
                  <el-select v-model="newAccountModel.cardConfData" class="w-100">
                    <el-option
                      v-for="item in isShowData"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="数据集可填个数" prop="cardDataNumber">
                  <el-input v-model="newAccountModel.cardDataNumber"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="组件预览图" prop="cardShowPic">
                  <el-upload
                    :headers="headers"
                    class="avatar-uploader"
                    v-model="newAccountModel.cardShowPic"
                    :action="$uploadUrl + upload"
                    :show-file-list="false"
                    :on-success="handleLicenseSuccess"
                    :before-upload="beforeAvatarUpload"
                    :accept="'image/jpg,image/jpeg,image/png'"
                  >
                    <img
                      v-if="newAccountModel.cardShowPic"
                      :src="newAccountModel.cardShowPic"
                      style="display: block;width: 100px;"
                      class="avatar"
                    />
                    <i
                      v-else
                      class="el-icon-plus avatar-uploader-icon"
                      :class="{ drop: detailsDisabled }"
                    ></i>
                  </el-upload>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="组件设置指导图" prop="cardConfPic">
                  <el-upload
                    :headers="headers"
                    class="avatar-uploader"
                    v-model="newAccountModel.cardConfPic"
                    :action="$uploadUrl + upload"
                    :show-file-list="false"
                    :on-success="handleLicenseSuccess2"
                    :before-upload="beforeAvatarUpload2"
                    :accept="'image/jpg,image/jpeg,image/png'"
                  >
                    <img
                      v-if="newAccountModel.cardConfPic"
                      :src="newAccountModel.cardConfPic"
                      style="display: block;width: 100px;"
                      class="avatar"
                    />
                    <i
                      v-else
                      class="el-icon-plus avatar-uploader-icon"
                      :class="{ drop: detailsDisabled }"
                    ></i>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>

          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button type="primary" :loading="addOrUpdateLoading" @click="dialogSave">确定</el-button>
        <el-button @click="$refs.ycDialog.dialogVisible = false">取消</el-button>
      </template>
    </yc-dialog>

    <!-- 封装操作后弹框 -->
    <global-message
      v-if="isGlobalMessageShow"
      @receive="receive"
      :message="message"
    />
  </div>
</template>

<script>
  import ycDialog from "@/components/pageDialog"
  import perButton from "@/components/permissionButton"
  import { getToken } from "@/utils/auth"
  import {
    queryPage,
    insertByModel,
    updateByModel,
    deleteByModel
  } from "@/api/basicsMessage/cardManage"

  export default {
    components: {
      ycDialog,
      perButton
    },
    data() {
      return {
        isGlobalMessageShow: false,
        message: "",
        tableData: [],
        count: 0,
        pageIndex: 1,
        pageSize: 10,
        loading: false,

        searchModel: {
          userName: "",
          status: null //正常/停用状态（全部）
        },

        newAccountModel: {
          cardName: "",
          cardCode: "",
          cardSize: "",
          cardPurpose: "",
          uiStyle: "",
          cardDes: '',
          cardConfTitle: '',
          cardTitleNumber: '',
          cardConfColor: '',
          cardColorNumber: '',
          cardConfIcon: '',
          cardIconNumber: '',
          cardConfInt: '',
          cardIntNumber: '',
          cardConfStatus: '',
          cardStatusNumber: '',
          cardConfControl: '',
          cardControlNumber: '',
          cardConfFlow: '',
          cardFlowNumber: '',
          cardConfData: '',
          cardDataNumber: '',
          cardShowPic: "",
          cardConfPic: "",
          id: 0
        },
        newAccountModelRules: {
          cardName: [{required: true, message: "请输入组件名称", trigger: "blur"}],
          cardCode: [{required: true, message: "请输入组件编号", trigger: "blur"}],
          cardSize: [{required: true, message: "请选择组件大小", trigger: "change"}],
          cardPurpose: [{required: true, message: "请选择组件用途", trigger: "change"}],
          uiStyle: [{required: true, message: "请选择组件主题", trigger: "change"}],
          cardConfTitle: [{required: true, message: "请选择标题设置", trigger: "change"}],
          cardConfColor: [{required: true, message: "请选择颜色设置", trigger: "change"}],
          cardConfIcon: [{required: true, message: "请选择图标设置", trigger: "change"}],
          cardConfInt: [{required: true, message: "请选择数值型变量设置", trigger: "change"}],
          cardConfStatus: [{required: true, message: "请选择状态型变量设置", trigger: "change"}],
          cardConfControl: [{required: true, message: "请选择控制参数设置", trigger: "change"}],
          cardConfFlow: [{required: true, message: "请选择流程设置", trigger: "change"}],
          cardConfData: [{required: true, message: "请选择数据集设置", trigger: "change"}],
          cardShowPic: [{required: true, message: "请上传组件预览图", trigger: "change"}],
          cardConfPic: [{required: true, message: "请上传组件设置指导图", trigger: "change"}]
        },
        headers: {
          token: getToken()
        },
        upload: "/uploadManager/upload",

        zjSizeListData: ['1*1', '2*1', '3*1', '4*1', '1*2', '2*2', '3*2', '4*2', '5*2', '6*2', '1*3', '2*3', '3*3', '4*3', '5*3', '6*3', '6*6', '12*6'],
        cardTypeListData: [
          {label: "PC", value: '0'},
          {label: "APP", value: '1'},
        ],
        styleTypeListData: [
          {label: "深色", value: '0'},
          {label: "浅色", value: '1'},
        ],
        isShowData: [
          {label: "是", value: "Y"},
          {label: "否", value: "N"},
        ],

        dialogTitle: "",
        centerTitle: "",
        detailsDisabled: false,
        addOrUpdateLoading: false
      }
    },
    created() {
      this.getAccountData()
    },
    methods: {
      /**
       * 获取表格方法
       */
      async getAccountData(tableInfo) {
        this.loading = true
        if (tableInfo) {
          const {userName, status} = tableInfo
          const {object} = await queryPage({
            pageIndex: this.pageIndex,
            pageSize: this.pageSize,
            userName,
            status
          })
          this.tableData = object.data
          this.count = object.count
          this.loading = false
        } else {
          const {object} = await queryPage({
            pageIndex: this.pageIndex,
            pageSize: this.pageSize
          })
          this.tableData = object.data
          this.count = object.count
          this.loading = false
        }
      },
      pagination(pageItem) {
        this.getAccountData(pageItem.page, pageItem.limit)
      },
      // 搜索按钮
      async searchClick() {
        this.getAccountData(this.searchModel)
      },


      /**
       * 删除按钮
       */
      async handlerDelete({id}) {
        this.$confirm("确定删除此数据吗", "删除操作", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "error",
          center: true
        }).then(async () => {
          const {message, status} = await deleteByModel({id})
          if (status == "200") {
            this.getAccountData()
            this.isGlobalMessageShow = true
            this.message = "success"
          } else {
            this.message = "error"
          }
        })
      },


      /**
       * 新建
       * */
      async handlerAccount() {
        this.dialogTitle = "添加组件"
        this.centerTitle = "添加组件"

        this.detailsDisabled = false
        this.addOrUpdateLoading = false
        this.$refs.ycDialog.dialogVisible = true

        Object.keys(this.newAccountModel).forEach((item) => {
          this.newAccountModel[item] = ""
        })
      },

      beforeAvatarUpload(file) {
        const suffix = file.type === "image/jpg" || file.type === "image/png" || file.type === "image/jpeg"
        if (!suffix) {
          this.$message({
            message: "请上传正确的图片格式！",
            type: "warning"
          })
          return false
        }
      },
      handleLicenseSuccess(res, file) {
        this.newAccountModel.cardShowPic = res.object.url
      },
      beforeAvatarUpload2(file) {
        const suffix = file.type === "image/jpg" || file.type === "image/png" || file.type === "image/jpeg"
        if (!suffix) {
          this.$message({
            message: "请上传正确的图片格式！",
            type: "warning"
          })
          return false
        }
      },
      handleLicenseSuccess2(res, file) {
        this.newAccountModel.cardConfPic = res.object.url
      },
      /**
       * 修改
       */
      handlerEdit(row) {
        this.dialogTitle = "编辑组件"
        this.centerTitle = "编辑组件"

        this.newAccountModel.id = row.id
        this.newAccountModel.cardName = row.cardName
        this.newAccountModel.cardCode = row.cardCode
        this.newAccountModel.cardSize = row.cardSize
        this.newAccountModel.cardPurpose = row.cardPurpose
        this.newAccountModel.uiStyle = row.uiStyle
        this.newAccountModel.cardDes = row.cardDes
        this.newAccountModel.cardConfTitle = row.cardConfTitle
        this.newAccountModel.cardTitleNumber = row.cardTitleNumber
        this.newAccountModel.cardConfColor = row.cardConfColor
        this.newAccountModel.cardColorNumber = row.cardColorNumber
        this.newAccountModel.cardConfIcon = row.cardConfIcon
        this.newAccountModel.cardIconNumber = row.cardIconNumber
        this.newAccountModel.cardConfInt = row.cardConfInt
        this.newAccountModel.cardIntNumber = row.cardIntNumber
        this.newAccountModel.cardConfStatus = row.cardConfStatus
        this.newAccountModel.cardStatusNumber = row.cardStatusNumber
        this.newAccountModel.cardConfControl = row.cardConfControl
        this.newAccountModel.cardControlNumber = row.cardControlNumber
        this.newAccountModel.cardConfFlow = row.cardConfFlow
        this.newAccountModel.cardFlowNumber = row.cardFlowNumber
        this.newAccountModel.cardConfData = row.cardConfData
        this.newAccountModel.cardDataNumber = row.cardDataNumber

        this.newAccountModel.cardShowPic = row.cardShowPic
        this.newAccountModel.cardConfPic = row.cardConfPic

        this.addOrUpdateLoading = false
        this.detailsDisabled = false
        this.$refs.ycDialog.dialogVisible = true
      },

      dialogSave() {
        this.$refs.newAccountFormRef.validate(async (v) => {
          if (v) {
            this.addOrUpdateLoading = true
            if (this.detailsDisabled) {
              // 详情的确定
              this.$refs.ycDialog.dialogVisible = false
            } else if (this.newAccountModel.id) {
              const {message, status} = await updateByModel({...this.newAccountModel})
              if (status == 200) {
                this.getAccountData()
                this.$refs.ycDialog.dialogVisible = false
                this.$message.success(message)
              }
            } else {
              const {message, status} = await insertByModel({...this.newAccountModel})
              if (status == 200) {
                this.getAccountData()
                this.$refs.ycDialog.dialogVisible = false
                this.$message.success(message)
              }
            }
            this.addOrUpdateLoading = false
          }
        })
      },


      /**
       * 刷新
       */
      refresh() {
        this.searchModel = {
          userName: "",
          status: null
        }
        this.getAccountData()
      },

      /**
       * globalMessage的知道按钮的方法
       * */
      receive() {
        this.isGlobalMessageShow = false
      },
    }
  }
</script>

<style lang="scss" scoped>
  .status {
    display: inline-block;
    padding: 0 10px;
  }

  .title-container {
    display: flex;
    justify-content: flex-end;
    height: 60px;

    .search-content {
      margin-left: 20px;
      display: flex;
    }

    .addAccount {
      margin: 10px 20px;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
  }


  .footer {
    height: 360px;
    display: flex;
    align-items: flex-end;
    // justify-content: st-end;
    padding: 5px 20px;
  }

  .dialog-content {
    padding-right: 15px;
  }
</style>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    /* cursor: no-drop; */
  }

  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }

  .drop {
    cursor: no-drop;
  }
</style>
